package top.huhuiyu.springboot3.aop;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
@Order(10)
@Aspect
public class LoggerAop implements BaseAop {

    @Before("controller()")
    public void before(JoinPoint jp) {
        log.debug("进入----------->{}", jp.getSignature());
        Object[] args = jp.getArgs();
        if (args == null || args.length == 0) {
            log.debug("无参数");
        } else {
            for (Object arg : args) {
                log.debug("参数：{}", arg);
            }
        }
    }

    @After("controller()")
    public void after(JoinPoint jp) {
        log.debug("完成----------->{}", jp.getSignature());
    }

    @AfterReturning(returning = "result", pointcut = "controller()")
    public void afterReturning(JoinPoint jp, Object result) {
        log.debug("{}返回----------->{}", jp.getSignature(), result);
    }

}
